<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveSupport::Notifications</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveSupport::Notifications 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activesupport/lib/active_support/notifications_rb.html">activesupport/lib/active_support/notifications.rb</a></li>
            
            <li><a href="../../files/activesupport/lib/active_support/notifications/fanout_rb.html">activesupport/lib/active_support/notifications/fanout.rb</a></li>
            
            <li><a href="../../files/activesupport/lib/active_support/notifications/instrumenter_rb.html">activesupport/lib/active_support/notifications/instrumenter.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<h1 id="label-Notifications"><a href="Notifications.html">Notifications</a></h1>

<p><code>ActiveSupport::Notifications</code> provides an instrumentation API
for Ruby.</p>

<h2 id="label-Instrumenters">Instrumenters</h2>

<p>To instrument an event you just need to do:</p>

<pre>ActiveSupport::Notifications.instrument('render', extra: :information) do
  render text: 'Foo'
end</pre>

<p>That executes the block first and notifies all subscribers once done.</p>

<p>In the example above <code>render</code> is the name of the event, and the
rest is called the <em>payload</em>. The payload is a mechanism that allows
instrumenters to pass extra information to subscribers. Payloads consist of
a hash whose contents are arbitrary and generally depend on the event.</p>

<h2 id="label-Subscribers">Subscribers</h2>

<p>You can consume those events and the information they provide by
registering a subscriber.</p>

<pre class="ruby"><span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">subscribe</span>(<span class="ruby-string">'render'</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">name</span>, <span class="ruby-identifier">start</span>, <span class="ruby-identifier">finish</span>, <span class="ruby-identifier">id</span>, <span class="ruby-identifier">payload</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">name</span>    <span class="ruby-comment"># =&gt; String, name of the event (such as 'render' from above)</span>
  <span class="ruby-identifier">start</span>   <span class="ruby-comment"># =&gt; Time, when the instrumented block started execution</span>
  <span class="ruby-identifier">finish</span>  <span class="ruby-comment"># =&gt; Time, when the instrumented block ended execution</span>
  <span class="ruby-identifier">id</span>      <span class="ruby-comment"># =&gt; String, unique ID for this notification</span>
  <span class="ruby-identifier">payload</span> <span class="ruby-comment"># =&gt; Hash, the payload</span>
<span class="ruby-keyword">end</span>
</pre>

<p>For instance, let’s store all “render” events in an array:</p>

<pre class="ruby"><span class="ruby-identifier">events</span> = []

<span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">subscribe</span>(<span class="ruby-string">'render'</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span>*<span class="ruby-identifier">args</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">events</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span><span class="ruby-operator">::</span><span class="ruby-constant">Event</span>.<span class="ruby-identifier">new</span>(*<span class="ruby-identifier">args</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>That code returns right away, you are just subscribing to “render” events.
The block is saved and will be called whenever someone instruments
“render”:</p>

<pre class="ruby"><span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">instrument</span>(<span class="ruby-string">'render'</span>, <span class="ruby-identifier">extra</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">information</span>) <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">render</span> <span class="ruby-identifier">text</span><span class="ruby-operator">:</span> <span class="ruby-string">'Foo'</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">event</span> = <span class="ruby-identifier">events</span>.<span class="ruby-identifier">first</span>
<span class="ruby-identifier">event</span>.<span class="ruby-identifier">name</span>      <span class="ruby-comment"># =&gt; &quot;render&quot;</span>
<span class="ruby-identifier">event</span>.<span class="ruby-identifier">duration</span>  <span class="ruby-comment"># =&gt; 10 (in milliseconds)</span>
<span class="ruby-identifier">event</span>.<span class="ruby-identifier">payload</span>   <span class="ruby-comment"># =&gt; { extra: :information }</span>
</pre>

<p>The block in the <code>subscribe</code> call gets the name of the event,
start timestamp, end timestamp, a string with a unique identifier for that
event (something like “535801666f04d0298cd6”), and a hash with the payload,
in that order.</p>

<p>If an exception happens during that particular instrumentation the payload
will have a key <code>:exception</code> with an array of two elements as
value: a string with the name of the exception class, and the exception
message.</p>

<p>As the previous example depicts, the class
<code>ActiveSupport::Notifications::Event</code> is able to take the
arguments as they come and provide an object-oriented interface to that
data.</p>

<p>It is also possible to pass an object as the second parameter passed to the
<code>subscribe</code> method instead of a block:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">ActionController</span>
  <span class="ruby-keyword">class</span> <span class="ruby-constant">PageRequest</span>
    <span class="ruby-keyword">def</span> <span class="ruby-identifier">call</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">started</span>, <span class="ruby-identifier">finished</span>, <span class="ruby-identifier">unique_id</span>, <span class="ruby-identifier">payload</span>)
      <span class="ruby-constant">Rails</span>.<span class="ruby-identifier">logger</span>.<span class="ruby-identifier">debug</span> [<span class="ruby-string">'notification:'</span>, <span class="ruby-identifier">name</span>, <span class="ruby-identifier">started</span>, <span class="ruby-identifier">finished</span>, <span class="ruby-identifier">unique_id</span>, <span class="ruby-identifier">payload</span>].<span class="ruby-identifier">join</span>(<span class="ruby-string">' '</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">subscribe</span>(<span class="ruby-string">'process_action.action_controller'</span>, <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">PageRequest</span>.<span class="ruby-identifier">new</span>)
</pre>

<p>resulting in the following output within the logs including a hash with the
payload:</p>

<pre>notification: process_action.action_controller 2012-04-13 01:08:35 +0300 2012-04-13 01:08:35 +0300 af358ed7fab884532ec7 {
   controller: &quot;Devise::SessionsController&quot;,
   action: &quot;new&quot;,
   params: {&quot;action&quot;=&gt;&quot;new&quot;, &quot;controller&quot;=&gt;&quot;devise/sessions&quot;},
   format: :html,
   method: &quot;GET&quot;,
   path: &quot;/login/sign_in&quot;,
   status: 200,
   view_runtime: 279.3080806732178,
   db_runtime: 40.053
 }
</pre>

<p>You can also subscribe to all events whose name matches a certain regexp:</p>

<pre class="ruby"><span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">subscribe</span>(<span class="ruby-regexp">/render/</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span>*<span class="ruby-identifier">args</span><span class="ruby-operator">|</span>
  <span class="ruby-operator">...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>and even pass no argument to <code>subscribe</code>, in which case you are
subscribing to all events.</p>

<h2 id="label-Temporary+Subscriptions">Temporary Subscriptions</h2>

<p>Sometimes you do not want to subscribe to an event for the entire life of
the application. There are two ways to unsubscribe.</p>

<p>WARNING: The instrumentation framework is designed for long-running
subscribers, use this feature sparingly because it wipes some internal
caches and that has a negative impact on performance.</p>

<h3 id="label-Subscribe+While+a+Block+Runs">Subscribe While a Block Runs</h3>

<p>You can subscribe to some event temporarily while some block runs. For
example, in</p>

<pre class="ruby"><span class="ruby-identifier">callback</span> = <span class="ruby-identifier">lambda</span> {<span class="ruby-operator">|</span>*<span class="ruby-identifier">args</span><span class="ruby-operator">|</span> <span class="ruby-operator">...</span> }
<span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">subscribed</span>(<span class="ruby-identifier">callback</span>, <span class="ruby-string">&quot;sql.active_record&quot;</span>) <span class="ruby-keyword">do</span>
  <span class="ruby-operator">...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>the callback will be called for all “sql.active_record” events instrumented
during the execution of the block. The callback is unsubscribed
automatically after that.</p>

<h3 id="label-Manual+Unsubscription">Manual Unsubscription</h3>

<p>The <code>subscribe</code> method returns a subscriber object:</p>

<pre class="ruby"><span class="ruby-identifier">subscriber</span> = <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>.<span class="ruby-identifier">subscribe</span>(<span class="ruby-string">&quot;render&quot;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span>*<span class="ruby-identifier">args</span><span class="ruby-operator">|</span>
  <span class="ruby-operator">...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>To prevent that block from being called anymore, just unsubscribe passing
that reference:</p>

<pre>ActiveSupport::Notifications.unsubscribe(subscriber)</pre>

<h2 id="label-Default+Queue">Default Queue</h2>

<p><a href="Notifications.html">Notifications</a> ships with a queue
implementation that consumes and publish events to log subscribers in a
thread. You can use any queue implementation you want.</p>

    </div>
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">CLASS</span>
          <a href="Notifications/Event.html">ActiveSupport::Notifications::Event</a>
        </li>
      
        <li>
          <span class="type">CLASS</span>
          <a href="Notifications/Fanout.html">ActiveSupport::Notifications::Fanout</a>
        </li>
      
        <li>
          <span class="type">CLASS</span>
          <a href="Notifications/Instrumenter.html">ActiveSupport::Notifications::Instrumenter</a>
        </li>
      
    </ul>
  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>I</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Notifications.html#method-c-instrument">instrument</a>,
              </li>
            
              
              <li>
                <a href="Notifications.html#method-c-instrumenter">instrumenter</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>P</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Notifications.html#method-c-publish">publish</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Notifications.html#method-c-subscribe">subscribe</a>,
              </li>
            
              
              <li>
                <a href="Notifications.html#method-c-subscribed">subscribed</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>U</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Notifications.html#method-c-unsubscribe">unsubscribe</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    
      <!-- Section attributes -->
      <div class="sectiontitle">Attributes</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class='attr-rw'>
              [RW]
            </td>
            <td class='attr-name'>notifier</td>
            <td class='attr-desc'></td>
          </tr>
        
      </table>
    


    <!-- Methods -->
    
      <div class="sectiontitle">Class Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-c-instrument">
            
              <b>instrument</b>(name, payload = {})
            
            <a href="Notifications.html#method-c-instrument" name="method-c-instrument" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-instrument_source')" id="l_method-c-instrument_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b/activesupport/lib/active_support/notifications.rb#L157" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-instrument_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/notifications.rb, line 157</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">instrument</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">payload</span> = {})
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">notifier</span>.<span class="ruby-identifier">listening?</span>(<span class="ruby-identifier">name</span>)
    <span class="ruby-identifier">instrumenter</span>.<span class="ruby-identifier">instrument</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">payload</span>) { <span class="ruby-keyword">yield</span> <span class="ruby-identifier">payload</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span> }
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">yield</span> <span class="ruby-identifier">payload</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-instrumenter">
            
              <b>instrumenter</b>()
            
            <a href="Notifications.html#method-c-instrumenter" name="method-c-instrumenter" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-instrumenter_source')" id="l_method-c-instrumenter_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b/activesupport/lib/active_support/notifications.rb#L180" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-instrumenter_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/notifications.rb, line 180</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">instrumenter</span>
  <span class="ruby-constant">InstrumentationRegistry</span>.<span class="ruby-identifier">instrumenter_for</span>(<span class="ruby-identifier">notifier</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-publish">
            
              <b>publish</b>(name, *args)
            
            <a href="Notifications.html#method-c-publish" name="method-c-publish" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-publish_source')" id="l_method-c-publish_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b/activesupport/lib/active_support/notifications.rb#L153" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-publish_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/notifications.rb, line 153</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">publish</span>(<span class="ruby-identifier">name</span>, *<span class="ruby-identifier">args</span>)
  <span class="ruby-identifier">notifier</span>.<span class="ruby-identifier">publish</span>(<span class="ruby-identifier">name</span>, *<span class="ruby-identifier">args</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-subscribe">
            
              <b>subscribe</b>(*args, &amp;block)
            
            <a href="Notifications.html#method-c-subscribe" name="method-c-subscribe" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-subscribe_source')" id="l_method-c-subscribe_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b/activesupport/lib/active_support/notifications.rb#L165" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-subscribe_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/notifications.rb, line 165</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">subscribe</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">notifier</span>.<span class="ruby-identifier">subscribe</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-subscribed">
            
              <b>subscribed</b>(callback, *args, &amp;block)
            
            <a href="Notifications.html#method-c-subscribed" name="method-c-subscribed" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-subscribed_source')" id="l_method-c-subscribed_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b/activesupport/lib/active_support/notifications.rb#L169" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-subscribed_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/notifications.rb, line 169</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">subscribed</span>(<span class="ruby-identifier">callback</span>, *<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">subscriber</span> = <span class="ruby-identifier">subscribe</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">callback</span>)
  <span class="ruby-keyword">yield</span>
<span class="ruby-keyword">ensure</span>
  <span class="ruby-identifier">unsubscribe</span>(<span class="ruby-identifier">subscriber</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-unsubscribe">
            
              <b>unsubscribe</b>(args)
            
            <a href="Notifications.html#method-c-unsubscribe" name="method-c-unsubscribe" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-unsubscribe_source')" id="l_method-c-unsubscribe_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b/activesupport/lib/active_support/notifications.rb#L176" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-unsubscribe_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/notifications.rb, line 176</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">unsubscribe</span>(<span class="ruby-identifier">args</span>)
  <span class="ruby-identifier">notifier</span>.<span class="ruby-identifier">unsubscribe</span>(<span class="ruby-identifier">args</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                        </div>

    </div>
  </body>
</html>    